package com.hardy.leetcode;

/**
 * 找出所有字符串的最长公共子串
 * User: Ruijie Date: 2016/9/8
 */
public class LongestCommonPrefix {

    public static String longestCommonPrefix(String[] strs) {
        if(strs.length==0){
            return "";
        }
        String prefix=strs[0];
        for(int i=1;i<strs.length;i++){
            prefix=getMaxPrefix(prefix,strs[i]);
        }
        return prefix;
    }

    private static String getMaxPrefix(String prefix, String str) {
        int minLen=Math.min(prefix.length(),str.length());
        String preString="";
        for(int i=0;i<minLen;i++){
            if(prefix.charAt(i)==str.charAt(i)){
                preString=str.substring(0,i+1);
            }else{
                break;
            }
        }
        return preString;
    }

    public static void main(String[] args) {
        String[] s={"aca","cba"};
        System.out.println(longestCommonPrefix(s));
    }
}
